﻿using _1_Interfaces.Interfaces.Cjw;
using _1_Interfaces.Models.Dtos.Cjw;
using _1_Interfaces.Models.Tables.Cjw;
using AutoMapper;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;

namespace _2_Services.Services.Cjw
{
    /// <summary>
    /// 通知栏表服务
    /// </summary>
    public class NoticeListService : INoticeListService
    {
        SqlSugarClient _db = CjwDbContext.GetInstance();
        private IMapper _mapper;

        public NoticeListService(IMapper mapper)
        {
            _mapper = mapper;
        }

        /// <summary>
        /// 获取通知栏列表
        /// </summary>
        /// <param name="pageIndex">第几页</param>
        /// <param name="pageSize">一页几条</param>
        /// <returns></returns>
        public NoticeListDto GetNoticeList(int pageIndex, int pageSize)
        {
            int totalCount = 0;
            NoticeListDto noticeListDto = new NoticeListDto();
            noticeListDto.NoticeLists = _db.Queryable<NoticeList>()
                .ToPageList(pageIndex, pageSize, ref totalCount);
            noticeListDto.Total = totalCount;
            noticeListDto.TotalPage = Tool.PageCount(totalCount, pageSize);
            return noticeListDto;
        }

        /// <summary>
        /// 新增通知栏内容
        /// </summary>
        /// <param name="noticeList">通知栏表实体，id不用传，后端赋值</param>
        /// <returns></returns>
        public bool AddNotice(NoticeList noticeList)
        {
            if (_db.Insertable(noticeList).ExecuteCommand() == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 编辑通知栏内容
        /// </summary>
        /// <param name="noticeList">通知栏表实体</param>
        /// <returns></returns>
        public bool EditNotice(NoticeList noticeList)
        {
            return _db.Updateable(noticeList).ExecuteCommandHasChange();
        }

        /// <summary>
        /// 删除通知栏内容 
        /// </summary>
        /// <param name="noticeId ">通知信息id</param>
        /// <returns></returns>
        public bool DelNotice(int noticeId)
        {
            return _db.Deleteable<NoticeList>()
                .Where(x => x.NoticeId == noticeId)
                .ExecuteCommandHasChange();
        }

        /// <summary>
        /// 获取通知栏
        /// </summary>
        /// <param name="noticeId">通知栏id</param>
        /// <returns></returns>
        public NoticeList GetNoticeByNoticeId(int noticeId)
        {
            return _db.Queryable<NoticeList>()
                .Where(x => x.NoticeId == noticeId)
                .First();
        }
    }
}
